package cjyyp.day01;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class synchronized自增案列 {
    static int time = 0;
    static final Object o1 = new Object();
    static final Object o2 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (o1) {
                    time++;
                }
            }
            log.debug("自增线程");
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (o1) {
                    time--;
                }
            }
            log.debug("自减线程");
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        log.debug("结果为{}", time);
    }
}
